package jlib.component.list
{
	import jlib.component.button.FrameButton;
	import jlib.component.button.LabelButton;
	import jlib.component.button.LinkButton;
	import jlib.component.button.SkinButton;
	import jlib.component.layout.LayoutDirection;
	import jlib.component.scroll.ScrollPane;
	import jlib.component.scroll.skin.ScrollBarSkinFactory;
	import jlib.utils.ArrangeUtil;
	import jlib.utils.DisplayObjectContainerUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	
	import mx.controls.Button;
	import mx.skins.spark.ScrollBarDownButtonSkin;
	
	public class ComboBox extends Sprite
	{
		static public const COMBOBOX_CHANGE:String = "COMBOBOX_CHANGE";
		private var _isVisible:Boolean = false;
		private var backgroundContainer:Sprite = new Sprite();
		private var valueContainer:Sprite = new Sprite();
		private var valueBtn:Sprite = new Sprite();
		private var valueLabelBtn:LabelButton;
		private var valueListItem:ButtonItem;
		private var selectListContainer:Sprite = new Sprite();
		private var listBox:ListBox;
		private var listBoxViewWidth:Number = 0;
		private var listBoxViewHeight:Number = 0;
		private var listBoxPane:ScrollPane;
		private var _width:Number;
		private var _height:Number;
		private var _itemHeight:Number;
		private var _selectedIndex:int = -1;
		private var m_interactable:Boolean = true;
		
		/**
		 * @param $width 컴퍼넌트 width 값.
		 * @param $height 컴퍼넌트 height 값.
		 * @param $itemHeight selectItem height 값.
		 * */
		public function ComboBox( $width:Number , $height:Number , $itemHeight:Number )
		{
			super();
			this._width = $width;
			this._height = $height;
			this._itemHeight = $itemHeight;
			
			setLayout();
			setEvent();
			
		}
		private function setLayout():void
		{
			listBoxViewWidth = _width;
			listBoxViewHeight = _itemHeight; 
			var button:LabelButton = new LabelButton();
			button.setLabel("선택하세요");
			button.setLabelSize(_width-ScrollBarSkinFactory.DEFAULT_SHORT_STRICT, _itemHeight);
			valueListItem = new ButtonItem(button);
			valueLabelBtn = new LabelButton();
			valueLabelBtn.setLabel( "▼" );
			valueLabelBtn.setSize(ScrollBarSkinFactory.DEFAULT_SHORT_STRICT, _itemHeight);
			valueBtn.addChild( valueLabelBtn );
			valueContainer.addChild( valueListItem );
			valueContainer.addChild( valueBtn );
			ArrangeUtil.horizontalSpace(valueContainer,0);
			
			listBoxPane = new ScrollPane();
			listBoxPane.setView( new Rectangle( 0 , 0 , _width , _height - _itemHeight ) );
			listBox = new ListBox();
			listBox.setView(new Rectangle(0, 0, _width - ScrollBarSkinFactory.DEFAULT_SHORT_STRICT, _height + _itemHeight));
			listBoxPane.setContent(listBox);
			listBoxPane.setHScrollBarVisible(false);
			listBoxPane.setVScrollBarVisible(false);
			selectListContainer.addChild( listBoxPane );
			selectListContainer.y = _itemHeight;
			
			this.addChild( backgroundContainer );
			this.addChild( valueContainer );
			this.addChild( selectListContainer );
			setListVisible();
		}
		private function setEvent():void
		{
			valueContainer.addEventListener( MouseEvent.CLICK , valueClick );
			listBox.addEventListener( MouseEvent.CLICK , itemListClick );
		}
		
		
		/**
		 * 리스트 아이템을 추가합니다. 
		 * @param $sprite Sprite.
		 */
		
		public function addItem($item:AbstractItem): void {
			if(!listBoxPane.getVerticalScrollBar().visible) { listBoxPane.setVScrollBarVisible(true); }
			listBox.addItem($item);
			listBoxViewWidth += $item.width;
			listBoxViewHeight += $item.height;
			listBox.setView(new Rectangle(0,0,listBoxViewWidth,listBoxViewHeight));
			/*
			var list:ButtonItem = new ButtonItem();
			list.addItem( $obj , _width , _itemHeight , 'left' );
			listBox.addItem( list );
			listBoxViewWidth += _width;
			listBoxViewHeight += _itemHeight;
			listBox.setView(new Rectangle(0,0,listBoxViewWidth,listBoxViewHeight));
			*/
		}
		
//		/**
//		 * String 으로 리스트 아이템을 추가합니다.
//		 * @param $str String.
//		 */	
//		public function addText( $str:String ):void
//		{
//			var list:ListItem = new ListItem();
//			list.addItem($str,_width,_itemHeight,'left');
//			listBox.addItem(list);
//			update();
//		}
		
		public function getScrollPane(): ScrollPane { return listBoxPane; }
		
		/**
		 * 현재 선택된 Index 값을 반환한다.
		 * */
		public function getSelectedIndex():int
		{
			return this._selectedIndex;
		}
		/**
		 * $idx 로 현재 선택된 값을 적용한다.
		 * @param $idx int
		 * */
		public function setSelectedIndex( $idx:int ):void
		{
			this._selectedIndex = $idx;
			listBox.selectAt( $idx );
			setSelectedIndexAction();
		}
		/**
		 * 추가된 AbstractListItem 인덱스로 부터 해당 AbstractListItem 을 가져온다.
		 * @param $idx int.
		 * */
		public function getChildrenByIndex( $idx:int ):AbstractItem
		{
			return listBox.getChildAt( $idx ) as AbstractItem;
		}
		
		/**
		 * 상단 백그라운드 스킨을 적용한다.
		 * @param $clip DisplayObject
		 * */
		public function setTopBackground( $clip:DisplayObject ):void
		{
			$clip.x = $clip.y = 0;
			backgroundContainer.addChild( $clip );
		}
		/**
		 * 백그라운드 스킨을 적용한다.
		 * @param $clip DisplayObject
		 * */
		public function setBackground( $clip:DisplayObject ):void
		{
			$clip.x = $clip.y = 0;
			listBoxPane.setBackground( $clip );
		}
		
		/**
		 * 입력 객체에 대한 스크롤바의 인터렉티브 (컨트롤) 유무를 반환합니다.
		 * @return boolean 객체.
		 */		
		public function getInteractivable(): Boolean { return m_interactable; }
		/**
		 * 입력 객체에 대한 스크롤바의 인터렉티브 (컨트롤) 유무를 설정합니다.
		 * @param $boolean
		 */
		public function setInteractivable($boolean:Boolean): void 
		{ 
			m_interactable = $boolean;
			//( $boolean ) ? ( valueListItem.getButton() as LabelButton ).buildDefault(_width-ScrollBarSkinFactory.DEFAULT_SHORT_STRICT,_itemHeight) : ( valueListItem.getButton() as LabelButton ).buildDisable(_width-ScrollBarSkinFactory.DEFAULT_SHORT_STRICT,_itemHeight);
		}
		
		public function setValueBtn($object:InteractiveObject): void {
			if($object) {
				valueBtn.removeChildAt(0);
				valueBtn.addChild($object);
			}
		}
		
		private function valueClick( $e:MouseEvent ):void
		{
			if( !m_interactable ) return;
			_isVisible = !_isVisible;
			setListVisible();
		}
		private function itemListClick( $e:MouseEvent ):void
		{
			if( !m_interactable ) return;
			this._selectedIndex = listBox.getSelectedIndex();
			setSelectedIndexAction();
//			_isVisible = false;
//			setListVisible();
//			setValue();
//			dispatchEvent( new Event( Event.SELECT ) );
		}
		private function setSelectedIndexAction():void
		{
			_isVisible = false;
			setListVisible();
			setValue();
			dispatchEvent( new Event( Event.SELECT ) );
		}
		private function setValue():void
		{
			DisplayObjectContainerUtil.removeAll( valueListItem );
			var bm:BitmapData = new BitmapData(_width-ScrollBarSkinFactory.DEFAULT_SHORT_STRICT,_itemHeight);
			bm.draw(listBox.getSelectedItem());
			var bmp:Bitmap = new Bitmap( bm );
			var sp:Sprite = new Sprite();
			sp.addChild( bmp );
			var button:SkinButton = new SkinButton(sp);
			valueListItem.addChild(button);
		}
		private function setListVisible():void
		{
			selectListContainer.visible = _isVisible;
			dispatchEvent( new Event( COMBOBOX_CHANGE ) );
		}
		public function set isVisible( $boo:Boolean ):void
		{
			_isVisible = $boo;
			selectListContainer.visible = _isVisible;
		}
		public function get isVisible():Boolean
		{
			return _isVisible;
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function get height():Number
		{
			return _height;
		}
	}
}